package com.example.calendarapp;

//import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;



import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


/**
 * Class representing the week fragment
 */

public class WeekFragment extends Fragment{

    private GestureDetector gestureDetector;
    private SimpleOnGestureListener listener;
    View.OnTouchListener gestureListener;

	
	WeekFragmentAdapter adapter;
	ExpandableListView elv;
	EventsDataSource data;
	
	/**
	 * On create, sets action bar value to week
	 *
	 * @param savedInstanceState the saved instance state
	 */
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		ActionBar actionBar = getActivity().getActionBar();
    	actionBar.setSelectedNavigationItem(1);
    	super.onCreate(savedInstanceState);
	}
	

	/**
	 * On create view, inflates layout, gets the adapter and sets up onclicklisteners for the expandable list view
	 * also sets up swipe detection object.
	 *
	 * @param inflater the inflater
	 * @param container the container
	 * @param savedInstanceState the saved instance state
	 * @return the view
	 */
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		 final OtherSwipeDetector sd = new OtherSwipeDetector(){
			@Override
			public void left()
			{
				adapter.incrementWeek();
				adapter.notifyDataSetChanged();
			}
			
			@Override
			public void right()
			{
				adapter.decrementWeek();
				adapter.notifyDataSetChanged();
			}
		};
        
		
		data = EventsDataSource.getInstance(getActivity());
		data.createDatabase();
		data.open();
		View v = inflater.inflate(R.layout.weekfragment, null);
		elv = (ExpandableListView) v.findViewById(R.id.WeekExpandView);
		adapter = new WeekFragmentAdapter(getActivity());
		elv.setAdapter(adapter);
		elv.setOnChildClickListener(new OnChildClickListener(){
			@Override
			public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2,
					int arg3, long arg4) {
				Event e = adapter.getChild(arg2, arg3);
				FragmentManager fm = getFragmentManager();
				FragmentTransaction ft = fm.beginTransaction();
				EventEditFragment ef = new EventEditFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("id", e.getId());
				bundle.putBoolean("repeated", (e.getRepeatDay() != -1));
				((Fragment)ef).setArguments(bundle);
				ft.replace(R.id.frag, ef);
				ft.addToBackStack(null); 
				ft.commit();
				return false;
			}			
		});
		elv.setOnGroupClickListener(new OnGroupClickListener(){
			@Override
			public boolean onGroupClick(ExpandableListView arg0, View v, int groupPosition, long id)
			{
			
				String dateString = adapter.getDateString(groupPosition);
				FragmentManager fm = getFragmentManager();
				FragmentTransaction ft = fm.beginTransaction();
				EventCreateFragment createFrag = new EventCreateFragment();
				Bundle bundle = new Bundle();
				bundle.putString("date", dateString);
				((Fragment)createFrag).setArguments(bundle);
				ft.replace(R.id.frag, createFrag);
				ft.addToBackStack(null);
				ft.commit();
				return false;
			}
		});
		elv.setOnTouchListener(sd);
		
		return v;
	}
	/**
	 * This method was for generating random test data and is deprecated
	 */
	public void add()
	{
		Random generator = new Random();
		
		for(int i = 0; i < 100; i++)
		{
//			data.add("RandomEvent", 2014, 10 + generator.nextInt(3), generator.nextInt(31), generator.nextInt(12), generator.nextInt(4), 1);
		}
	}

}